package com.example.demo4.service;

import com.example.demo4.entity.Employee;
import com.example.demo4.entity.Schema;
import com.example.demo4.entity.Schema_number;
import com.example.demo4.entity.Year_date_all;
import com.example.demo4.mapper.DateMapper;
import com.example.demo4.mapper.EmployeeMapper;
import com.example.demo4.mapper.SchemaMapper;
import com.example.demo4.mapper.Schema_numberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author: lin
 * @description:
 * @time: 2021/8/1 15:49
 */
@Service
public class SchemaService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SchemaMapper schemaMapper;
    @Autowired
    private Schema_numberMapper schemaNumberMapper;
    @Autowired
    private DateMapper dateMapper;
    //通过日期排班
    public void schmaBydate(Date date, int morningNum, int noonNum, int nightNum) {

        //获取员工信息
        List<Employee> employees = employeeMapper.selectAll();
        Collections.shuffle(employees);
        //需要排班的人数
        int num = morningNum + noonNum + nightNum;

        Schema schema = new Schema();
        Schema_number schemaOfNumber = new Schema_number();
        Year_date_all yearDateAll = new Year_date_all();
        //早班排班
        for (int i = 0; i < morningNum; i++) {
            schema.setClasses(Integer.valueOf(1));
            schema.setPdate(date);
            int eid = employees.get(i).getId();
            schema.setEid(Integer.valueOf(eid));
            //人员排班表添加信息
            schemaMapper.save(schema);
            //员工表添加排班次数
            int schemaNumber = 0;
            System.out.println(employees.get(i));
            Employee employee = employees.get(i);
            schemaNumber = employee.getSchemaNumber();
            System.out.println(schemaNumber);

            employee.setSchemaNumber(++schemaNumber);   //schemaNumber++为先执行语句再加加
            System.out.println(employee);
            employeeMapper.update(employee);
        }
        //
        //早班次排班表添加信息
        schemaOfNumber.setSnDate(date);
        schemaOfNumber.setSnClasses(1);
        schemaOfNumber.setNumber(morningNum);
        schemaNumberMapper.save(schemaOfNumber);
        //中班排班
        for (int i = morningNum; i < morningNum + noonNum; i++) {
            schema.setClasses(2);
            schema.setPdate(date);
            schema.setEid(employees.get(i).getId());
            //人员排班表添加信息
            schemaMapper.save(schema);
            //员工表添加排班次数
            int schemaNumber = employees.get(i).getSchemaNumber();
            employees.get(i).setSchemaNumber(++schemaNumber);
            employeeMapper.update(employees.get(i));
        }
        //中班次排班表添加信息
        schemaOfNumber.setSnDate(date);
        schemaOfNumber.setSnClasses(2);
        schemaOfNumber.setNumber(noonNum);
        schemaNumberMapper.save(schemaOfNumber);
        //晚班排班
        for (int i = morningNum + noonNum; i < morningNum + noonNum + nightNum; i++) {
            schema.setClasses(3);
            schema.setPdate(date);
            schema.setEid(employees.get(i).getId());
            //人员排班表添加信息
            schemaMapper.save(schema);
            //员工表添加排班次数
            int schemaNumber = employees.get(i).getSchemaNumber();
            employees.get(i).setSchemaNumber(++schemaNumber);
            employeeMapper.update(employees.get(i));
        }
        //晚次排班表添加信息
        schemaOfNumber.setSnDate(date);
        schemaOfNumber.setSnClasses(3);
        schemaOfNumber.setNumber(nightNum);
        schemaNumberMapper.save(schemaOfNumber);
        //日期表修改排班状态
        yearDateAll.setRepDate(date);
        yearDateAll.setIsSchema(1);
        dateMapper.update(yearDateAll);

    }
    //根据日期显示排班人员信息
    public List showList(Date pdate) {
        return schemaMapper.selectAllByDate(pdate);
    }

    public int save(Date date, String name, int id, int classes) {
        //添加员工排班表信息
        //Employee employee = new Employee();
        Schema schema = new Schema();
        schema.setPdate(date);
        schema.setEid(id);
        schema.setClasses(classes);
        schemaMapper.save(schema);
        //修改班次排班表信息 对应班次+1
        Schema_number snInfo = schemaNumberMapper.getSNInfo(date, classes);
        int num = snInfo.getNumber();
        snInfo.setNumber(++num);
        schemaNumberMapper.update(snInfo);
        //修改员工表排班次数
        Employee employee = employeeMapper.getEmployeeInfo(id);
        int schemaNumber = employee.getSchemaNumber();
        employee.setSchemaNumber(++schemaNumber);
        employeeMapper.update(employee);
        return 0;
    }
    //删除排班信息
    public int delectSchema(Date date, String name, int eid, int classes) {
        //如果员工排班信息存在

        //删除员工排班信息
        schemaMapper.deleteByEidDateClasses(date,classes,eid);
        //修改班次排班信息 对应班次-1
        Schema_number snInfo = schemaNumberMapper.getSNInfo(date, classes);
        int num = snInfo.getNumber();
        snInfo.setNumber(--num);
        schemaNumberMapper.update(snInfo);
        //修改员工排班次数 次数-1
        Employee employee = employeeMapper.getEmployeeInfo(eid);
        int schemaNumber = employee.getSchemaNumber();
        employee.setSchemaNumber(--schemaNumber);
        employeeMapper.update(employee);
        return 0;
    }
    //更新排班信息
    public int updateSchema(Date date, int eid, int classes) {
        //获取排班信息
        Schema schemaInfoByDEC = schemaMapper.getSchemaInfoByDEC(date, classes, eid);

        schemaInfoByDEC.setClasses(classes);
        //如果员工有更新，修改员工排班次数
        if(schemaInfoByDEC.getEid()!=eid){
            Employee employee = employeeMapper.getEmployeeInfo(eid);
            int schemaNumber = employee.getSchemaNumber();
            employee.setSchemaNumber(--schemaNumber);
            employeeMapper.update(employee);
            schemaInfoByDEC.setEid(eid);
        }
        schemaMapper.update(schemaInfoByDEC);


        return 0;
    }
}
